package com.ny.service.impl;

import com.ny.entity.Device1;
import com.ny.entity.Result;
import com.ny.mapper.DeviceRepository;
import com.ny.service.DeviceService1;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;
@Service
public class DeviceServiceimpl1 implements DeviceService1 {

    @Autowired
    private DeviceRepository deviceRepository;
    public List<Device1> getAllDevices() {
        return deviceRepository.findAll();
    }

    @Override
    public List<Device1> findDeviceByname(String name) {return deviceRepository.findDeviceByname( name);}

    @Override
    public Result addDevice(Device1 device) {
        // 检查设备编号是否已存在
        Optional<Device1> existingDevice = deviceRepository.findByDeviceNo(device.getDeviceNo());
        if (existingDevice.isPresent()) {
            return Result.error("设备编号已存在");
        }
        // 保存设备
        Device1 savedDevice = deviceRepository.save(device);
        if (savedDevice != null) {
            return Result.success("添加成功");
        } else {
            return Result.error("添加失败");
        }
    }

    @Override
    public Optional<Device1> findByDeviceNo(String deviceNo) {
        return deviceRepository.findByDeviceNo(deviceNo);
    }

    @Override
    public Result updateDevice(String id, Device1 device) {
        // 根据ID查找设备
        Optional<Device1> existingDevice = deviceRepository.findById(id);
        if (existingDevice.isPresent()) {
            // 更新设备信息
            Device1 updatedDevice = existingDevice.get();
            updatedDevice.setName(device.getName());
            updatedDevice.setType(device.getType());
            updatedDevice.setStatus(device.getStatus());
            updatedDevice.setFactory(device.getFactory());
            updatedDevice.setDeviceNo(device.getDeviceNo());
            
            // 保存更新后的设备
            deviceRepository.save(updatedDevice);
            return Result.success("更新成功");
        } else {
            return Result.error("设备不存在");
        }
    }

    @Override
    public Result updateDeviceByDeviceNo(String deviceNo, Device1 device) {
        // 根据deviceNo查找设备
        Optional<Device1> existingDevice = deviceRepository.findByDeviceNo(deviceNo);
        if (existingDevice.isPresent()) {
            // 更新设备信息
            Device1 updatedDevice = existingDevice.get();
            updatedDevice.setName(device.getName());
            updatedDevice.setType(device.getType());
            updatedDevice.setStatus(device.getStatus());
            updatedDevice.setFactory(device.getFactory());
            updatedDevice.setDeviceNo(device.getDeviceNo());
            
            // 保存更新后的设备
            deviceRepository.save(updatedDevice);
            return Result.success("更新成功");
        } else {
            return Result.error("设备不存在");
        }
    }

    @Override
    public Result deleteDeviceById(String id) {
        try {
            // 检查设备是否存在
            Optional<Device1> existingDevice = deviceRepository.findById(id);
            if (existingDevice.isPresent()) {
                // 删除设备
                deviceRepository.deleteById(id);
                return Result.success("删除成功");
            } else {
                return Result.error("设备不存在");
            }
        } catch (Exception e) {
            return Result.error("删除失败: " + e.getMessage());
        }
    }

    @Override
    public List<Device1> findDevicesByNameContaining(String name) {
        return deviceRepository.findByNameContaining(name);
    }




}